import { request } from "@/utils/request";
import {
    CreateDictionaryReq,
    PageReq,
    UpdateDictionaryReq,
    Dictionary,
    DictionaryItem,
    CreateDictionaryItemReq,
    UpdateDictionaryItemReq,
    IDParams,
    PageObj,
    AllDictionaryRecord,
} from "@/typings/api/index";

const dictionaryUrl = "/dictionary";

export const getDictionaries = (params: PageReq<Dictionary>) => request.get<PageObj<Dictionary>>(dictionaryUrl, { params });

export const getAllDictionary = () => request.get<AllDictionaryRecord[]>(`${dictionaryUrl}/all`);

export const getDictionary = (_id: string) => request.get<Dictionary>(`${dictionaryUrl}/${_id}`);

export const getDictionaryByKey = (key: string) => request.get<Dictionary>(`${dictionaryUrl}/getDictionaryByKey`, { params: { key } });

export const createDictionary = (data: CreateDictionaryReq) => request.post(dictionaryUrl, data);

export const updateDictionary = (data: UpdateDictionaryReq) => request.put(dictionaryUrl, data);

export const deleteDictionary = (_id: string) => request.delete(dictionaryUrl, { data: { _id } });

const dictionaryItemsUrl = "/dictionary/items";

export const getDictionaryItems = (params: PageReq<IDParams & { key: string }>) => request.get(dictionaryItemsUrl, { params });

export const getDictionaryItem = (_id: string) => request.get<DictionaryItem>(`${dictionaryItemsUrl}/${_id}`);

export const createDictionaryItem = (data: CreateDictionaryItemReq) => request.post(dictionaryItemsUrl, data);

export const updateDictionaryItem = (data: UpdateDictionaryItemReq) => request.put(dictionaryItemsUrl, data);

export const deleteDictionaryItem = (_id: string) => request.delete(dictionaryItemsUrl, { data: { _id } });
